package com.dkd.flow.strategy.service.operation;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSONObject;
import com.dkd.common.core.domain.entity.SysUser;
import com.dkd.flow.context.ProcessContext;
import com.dkd.flow.context.ProcessContextBuilder;
import com.dkd.flow.domain.EProcessInstances;
import com.dkd.flow.domain.EProcessTask;
import com.dkd.flow.emums.FlowOperation;
import com.dkd.flow.emums.FlowStatus;
import com.dkd.flow.emums.TaskActionType;
import com.dkd.flow.emums.TaskStatus;
import com.dkd.flow.service.impl.EProcessTaskServiceImpl;
import com.dkd.flow.strategy.annotation.Strategy;
import com.dkd.flow.utils.RevokeUtils;
import com.dkd.flow.utils.TaskCreate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Strategy(FlowOperation.RETURNED)
public class ReturnStrategy extends BaseStrategy {
        @Autowired
        ProcessContextBuilder processContextBuilder;
        @Autowired
        TaskCreate taskCreate;
        @Autowired
        private EProcessTaskServiceImpl eProcessTaskServiceImpl;
        @Autowired
        RevokeUtils revokeUtils;

        @Override
        public Long execute(JSONObject data) {
                // 使用构建器初始化上下文 --- start --- //
                ProcessContext context = processContextBuilder.build(data);
                EProcessInstances eProcessInstances = context.getEProcessInstances();
                EProcessTask curTask = context.getCurTask();
                Long curTaskId = context.getCurTaskId();
                Long curTaskStatus = context.getCurTaskStatus();
                EProcessInstances paramsEProcessInstances = context.getEProcessInstances();
                JSONObject formValue = context.getFormValue();
                Long processDefinitionId = context.getProcessDefinitionId();
                // 使用构建器初始化上下文 --- end --- //
                Long taskGroupId = taskCreate.getTaskGroupId(eProcessInstances);
                // 放 createEProcessInstances 后面，不然找不到实例id
                initNodeRelations(processDefinitionId);
                // 创建发起人重新发起的任务
                JSONObject launchNode = links.get(0)
                                .getJSONObject("toNode");
                if (launchNode == null) {
                        throw new RuntimeException("links 中第一个节点的 toNode 为 null，无法创建发起人任务");
                }
                SysUser liableUser = nodeUtils.getLiableUser(eProcessInstances);
                // 创建 审批人任务 APPROVER_TASK 2 自己 已驳回 RETURNED 6 驳回 RETURNED 5
                Long approverTaskId = IdUtil.getSnowflakeNextId();
                EProcessTask approverTask = taskCreate.approverTask(eProcessInstances,
                                nodeUtils.getApproverByTask(curTask),
                                curTask.getProcessDesignId(),
                                TaskStatus.RETURNED,
                                approverTaskId,
                                approverTaskId,
                                null,
                                taskGroupId,
                                JSONObject.toJSONString(formValue),
                                TaskActionType.OPERATION_TASK);
                // 创建 审批人任务 APPROVER_TASK 2 并行 已取消 CANCELED 9 驳回 RETURNED 5
                revokeUtils.createSynTasksWithStatus(curTask,
                                eProcessInstances,
                                TaskStatus.CANCELED,
                                approverTaskId,
                                taskGroupId,
                                formValue,
                                TaskActionType.LOG_TASK);
                // 创建 发起人任务 INITIATOR_TASK 1 下一个 审批中 UNDER_APPROVAL 2
                // 上一个任务的id，就是新建的驳回任务的id
                Long launchTaskkId = IdUtil.getSnowflakeNextId();
                taskCreate.createLaunchTask(eProcessInstances,
                                launchNode,
                                null,
                                TaskStatus.UNDER_APPROVAL,
                                launchTaskkId,
                                null,
                                approverTaskId,
                                liableUser,
                                taskGroupId,
                                JSONObject.toJSONString(formValue),
                                TaskActionType.LOG_TASK);
                // 驳回 RETURNED 5
                eProcessInstances.setFlowStatus(FlowStatus.RETURNED.getValue()
                                .longValue());
                updateEProcessInstances(eProcessInstances);
                // 清除操作组ID
                taskCreate.clearOperationGroupId(eProcessInstances);
                return approverTaskId;
        }
}